from sql_operation import MySqlOperation


class RegistHandle:
    def __init__(self, user_name, passwd, email, conn=None, description=None):
        self.conn = conn
        self.uname = user_name
        self.pwd = passwd
        self.email = email
        self.cur = MySqlOperation()
        self.msgr = ''
        self.teacher_info = description

    def regist_handle(self, tb_name='stu_info', col_name='stu_name', col_email='stu_email', col_pwd='stu_pwd',
                      col_intro='stu_descript'):

        user_name_exists = 'select %s from %s where %s=\'%s\'' \
                           % (col_name, tb_name, col_name, self.uname)

        user_email_count = 'select count(%s) from %s ' \
                           'where %s=\'%s\' group by %s' \
                           % (col_name, tb_name, col_email, self.email, col_email)
        print(user_email_count)

        self.cur.operation(user_name_exists)
        is_exists = self.cur.fetch_all()

        if not is_exists:
            self.cur.operation(user_email_count)
            email_amount = self.cur.fetch_all()

            if email_amount:
                if email_amount[0][0] > 3:
                    self.msgr = '406:邮箱注册次数过多, 请更换邮箱'.encode()
                else:
                    self.insert_to_db(tbname=tb_name,
                                      col_name=col_name, col_pwd=col_pwd, col_email=col_email,
                                      val_name=self.uname, val_pwd=self.pwd, val_email=self.email,
                                      col_teacher_info='teacher_intro', val_teacher_info=col_intro
                                      )
                    self.msgr = '200:注册成功'.encode()

            else:
                self.insert_to_db(tbname=tb_name,
                                  col_name=col_name, col_pwd=col_pwd, col_email=col_email,
                                  val_name=self.uname, val_pwd=self.pwd, val_email=self.email,
                                  col_teacher_info='teacher_intro', val_teacher_info=col_intro)
                self.msgr = '200:注册成功'.encode()

        else:
            self.msgr = '406:用户名已经存在'.encode()

        print('msgr =', self.msgr)
        self.conn.send(self.msgr)

    def teacher_regist_handle(self):
        self.regist_handle(tb_name='teacher_info',
                           col_name='teacher_name', col_pwd='teacher_pwd', col_email='teacher_email',
                           col_intro=self.teacher_info)

    def insert_to_db(self, tbname='stu_info', **kwargs):
        if tbname == 'stu_info':
            insert_stu_info = 'insert into %s(%s, %s, %s) ' \
                              'values(\'%s\', \'%s\', \'%s\')' \
                              % (tbname,
                                 kwargs['col_name'], kwargs['col_pwd'], kwargs['col_email'],
                                 kwargs['val_name'], kwargs['val_pwd'], kwargs['val_email'])
        else:
            insert_stu_info = 'insert into %s(%s, %s, %s, %s) ' \
                              'values(\'%s\', \'%s\', \'%s\', \'%s\')' \
                              % (tbname,
                                 kwargs['col_name'], kwargs['col_pwd'], kwargs['col_email'], kwargs['col_teacher_info'],
                                 kwargs['val_name'], kwargs['val_pwd'], kwargs['val_email'], kwargs['val_teacher_info'])

        try:
            self.cur.operation(insert_stu_info)

        except Exception as sql_e:
            print(sql_e)
            self.cur.rollback()

        else:
            self.cur.commit()

        finally:
            self.cur.close_all()  # 换成进程，这个就是需要加上的


if __name__ == '__main__':
    r = RegistHandle(user_name='laoshi', passwd='123', email='123@qq.com', description='i am a teacher')
    r.teacher_regist_handle()
